package com.sc.nft.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.MetaMapSettleDao;
import com.sc.nft.dao.OrderDao;
import com.sc.nft.dao.UserEcologyExpenditureRecordDao;
import com.sc.nft.dto.UserEcologyHatchRewardsRecordsDao;
import com.sc.nft.entity.MetaMapSettle;
import com.sc.nft.entity.Order;
import com.sc.nft.entity.UserEcologyExpenditureRecord;
import com.sc.nft.entity.UserEcologyHatchRewardsRecords;
import com.sc.nft.entity.vo.metamap.MetaMapDividendsVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.MessageQueueHelper;
import com.sc.nft.service.MetaMapSettleService;
import com.sc.nft.service.UserEcologyPointsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Slf4j
@RequiredArgsConstructor
@Service
public class MetaMapSettleServiceImpl implements MetaMapSettleService {

    private final MetaMapSettleDao metaMapSettleDao;
    private final UserEcologyExpenditureRecordDao ecologyExpenditureRecordDao;
    private final UserEcologyPointsService userEcologyPointsService;
    private final UserEcologyHatchRewardsRecordsDao userEcologyHatchRewardsRecordsDao;
    private final MessageQueueHelper messageQueueHelper;
    private final OrderDao orderDao;

    @Override
    public void sendMsg() {
        //商城消息
//        MetaMapSettleDTO metaMapSettleDTO = new MetaMapSettleDTO();
//        metaMapSettleDTO.setWishBalancePrice(new BigDecimal(0));
//        MetaMapSettleDTO mall = metaMapSettleDTO.mall("20240926152903745907669", "分润模板", 42879L, MetaMapSettleSourceTypeEnum.MALL, new BigDecimal(50), BigDecimal.ZERO, 28L, "山东省", "菏泽市", "单县");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, mall, MessageTopic.META_MAP_SETTLE, "20240926152903745907669");


        //店铺消息
//        MetaMapSettleDTO shopMessage = new MetaMapSettleDTO();
//        shopMessage.setOrderNo("20240909085123739973792");
//        shopMessage.setCharge(new BigDecimal(120));
//        shopMessage.setParticipation(new BigDecimal(120));
//        shopMessage.setBuyUserId(116L);
//        shopMessage.setSourceType(MetaMapSettleSourceTypeEnum.SHOP);
//        shopMessage.setShareProfitPanelId(1L);
//        shopMessage.setTransName("我买了个猪店铺");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, shopMessage, MessageTopic.META_MAP_SETTLE, "20240909085123739973792");

        //道具消息
//        MetaMapSettleDTO shopMessage = new MetaMapSettleDTO();
//        shopMessage.setOrderNo("20240909085123739973792");
//        shopMessage.setCharge(new BigDecimal(120));
//        shopMessage.setParticipation(new BigDecimal(120));
//        shopMessage.setBuyUserId(116L);
//        shopMessage.setSourceType(MetaMapSettleSourceTypeEnum.PROP_ORDER);
//        shopMessage.setShareProfitPanelId(1L);
//        shopMessage.setTransName("我买了个猪道具");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, shopMessage, MessageTopic.META_MAP_SETTLE, "20240909085123739973792");

        //熔炼消息
//        MetaMapSettleDTO shopMessage = new MetaMapSettleDTO();
//        shopMessage.setOrderNo("20240909085123739973792");
//        shopMessage.setCharge(new BigDecimal(120));
//        shopMessage.setParticipation(new BigDecimal(120));
//        shopMessage.setBuyUserId(116L);
//        shopMessage.setSourceType(MetaMapSettleSourceTypeEnum.SMELTING);
//        shopMessage.setTransName("我买了个猪熔炼");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, shopMessage, MessageTopic.META_MAP_SETTLE, "20240909085123739973792");



        //一级
//        MetaMapSettleDTO shopMessage = new MetaMapSettleDTO();
//        shopMessage.setOrderNo("20240927142843057234756");
//        shopMessage.setTransName("CHEN-首发版权分润");
//        shopMessage.setBuyUserId(42879L);
//        shopMessage.setParticipation(new BigDecimal(100));
//        shopMessage.setCharge(new BigDecimal(0));
//        shopMessage.setShareProfitPanelId(45L);
//        shopMessage.setSourceType(MetaMapSettleSourceTypeEnum.FIRST_ORDER);
//        shopMessage.setSendTime("2024-09-27 14:28:48");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, shopMessage, MessageTopic.META_MAP_SETTLE, "20240927142843057234756");

        //二级
//        MetaMapSettleDTO shopMessage = new MetaMapSettleDTO();
//        shopMessage.setOrderNo("20240909085123739973792");
//        shopMessage.setCharge(new BigDecimal(120));
//        shopMessage.setParticipation(new BigDecimal(120));
//        shopMessage.setBuyUserId(116L);
//        shopMessage.setShareProfitPanelId(10L);
//        shopMessage.setSourceType(MetaMapSettleSourceTypeEnum.SECOND_ORDER);
//        shopMessage.setTransName("我买了个猪二级");
//        messageQueueHelper.sendMessage(MessageTopic.META_MAP_SETTLE, shopMessage, MessageTopic.META_MAP_SETTLE, "20240909085123739973792");


        //一级直推
//        Order order = orderDao.getByOrderNo("20241031160253207139672");
//        messageQueueHelper.sendMessage(MessageTopic.FIRST_LEVEL_DIRECT_PUSH_INCOME, order);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void distributeFuel() {
        List<MetaMapSettle> metaMapSettles = metaMapSettleDao.listSmelting();
        for (MetaMapSettle metaMapSettle : metaMapSettles) {
            metaMapSettle.setPaymentTime(new Date());
            //新增积分
            if (!userEcologyPointsService.addEcologyPoints(metaMapSettle.getEarningsUserId(), metaMapSettle.getQuota(), EcologyPointsEnum.INCOME, StrUtil.EMPTY)) {
                throw new GlobalRunTimeException("积分新增失败!");
            }
            //保存收入记录表
            UserEcologyExpenditureRecord userEcologyExpenditureRecord = getUserEcologyExpenditureRecord(metaMapSettle);
            if (!ecologyExpenditureRecordDao.save(userEcologyExpenditureRecord)) {
                throw new GlobalRunTimeException("收入记录表保存失败");
            }
            //保存用户奖励记录表
            UserEcologyHatchRewardsRecords userEcologyHatchRewardsRecords = getUserEcologyHatchRewardsRecords(metaMapSettle);
            if (!userEcologyHatchRewardsRecordsDao.save(userEcologyHatchRewardsRecords)) {
                throw new GlobalRunTimeException("用户奖励记录保存失败");
            }
            //更新发放状态
            if (!metaMapSettleDao.updateSettleSendStatus(metaMapSettle.getId(), metaMapSettle.getVersion(), MetaMapSettleStatusEnum.ISSUED, metaMapSettle.getPaymentTime())) {
                throw new GlobalRunTimeException("发放状态更新失败!");
            }
        }
    }

    private static UserEcologyExpenditureRecord getUserEcologyExpenditureRecord(MetaMapSettle metaMapSettle) {
        UserEcologyExpenditureTypeEnum ecologyExpenditureType = UserEcologyExpenditureTypeEnum.ECOLOGY_POINTS;
        UserEcologyExpenditureRecord userEcologyExpenditureRecord = new UserEcologyExpenditureRecord();
        userEcologyExpenditureRecord.setUserId(metaMapSettle.getEarningsUserId());
        userEcologyExpenditureRecord.setType(ecologyExpenditureType);
        userEcologyExpenditureRecord.setRemark(metaMapSettle.getMetaMap());
        userEcologyExpenditureRecord.setTypeName(ecologyExpenditureType.getDescription());
        userEcologyExpenditureRecord.setLinkId(0L);
        userEcologyExpenditureRecord.setLinkName(UserEcologyExpenditureTypeEnum.ECOLOGY_POINTS.getDescription());
        userEcologyExpenditureRecord.setLinkCount(metaMapSettle.getQuota());
        userEcologyExpenditureRecord.setDateTime(Integer.parseInt(DateUtil.format(metaMapSettle.getPaymentTime(), "yyyyMMdd")));
        userEcologyExpenditureRecord.setLinkImage(StrUtil.EMPTY);
        return userEcologyExpenditureRecord;
    }

    private static UserEcologyHatchRewardsRecords getUserEcologyHatchRewardsRecords(MetaMapSettle metaMapSettle) {
        EcologyRewardsType ecologyPoints = EcologyRewardsType.ECOLOGY_POINTS;
        UserEcologyHatchRewardsRecords userEcologyHatchRewardsRecords = new UserEcologyHatchRewardsRecords();
        userEcologyHatchRewardsRecords.setUserId(metaMapSettle.getEarningsUserId());
        userEcologyHatchRewardsRecords.setUserEcologyHatchRecordsId(0L);
        userEcologyHatchRewardsRecords.setEcologyHatchId(Long.parseLong(metaMapSettle.getOrderNo()));
        userEcologyHatchRewardsRecords.setRewardsType(ecologyPoints);
        userEcologyHatchRewardsRecords.setRewardsTypeName(ecologyPoints.getDescription());
        userEcologyHatchRewardsRecords.setIncomeType(EcologyIncomeTypeEnum.MATE_MAP);
        userEcologyHatchRewardsRecords.setIncomeTypeName(EcologyIncomeTypeEnum.MATE_MAP.getDescription());
        userEcologyHatchRewardsRecords.setLinkId(0L);
        userEcologyHatchRewardsRecords.setLinkName(UserEcologyExpenditureTypeEnum.ECOLOGY_POINTS.getDescription());
        userEcologyHatchRewardsRecords.setLinkCount(metaMapSettle.getQuota());
        userEcologyHatchRewardsRecords.setLinkImge(StrUtil.EMPTY);
        return userEcologyHatchRewardsRecords;
    }

    @Override
    public IPage<MetaMapDividendsVO> pageFundDividends(Integer pageNo, Integer pageSize, MetaMapSettleSourceTypeEnum dividendsTypeEnum, String name, String userTel, Date startTime, Date endTime, MetaMapSettleStatusEnum status) {
        Page<MetaMapDividendsVO> metaMapDividendsVOPage = metaMapSettleDao.pageFundDividends(pageNo, pageSize, dividendsTypeEnum, name, userTel, startTime, endTime, status);
        return metaMapDividendsVOPage.convert(x -> {
            x.setStatusName(x.getStatus().getDesc());
            x.setSourceTypeName(x.getSourceType().getDesc());
            return x;
        });
    }
}




